package vip.xiaonuo.device.modular.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.util.SignTool;
import vip.xiaonuo.device.modular.mapper.UserMapper;
import vip.xiaonuo.device.modular.service.TencentMinCardService;
import vip.xiaonuo.device.vo.CabinetScreenUserVO;
import vip.xiaonuo.device.vo.Result;

import java.time.Instant;
import java.util.*;
@Slf4j
@ConfigurationProperties(prefix = "tencent.weixiao")
@Service
public class TencentMinCardServiceImpl implements TencentMinCardService {

    @Setter
    private String app_key;
    @Setter
    private String app_secret;
    @Setter
    private String base_url;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result getuserinfobycode(String code) {
        JSONObject params = JSON.parseObject(code);
        //创建请求参数
        Map<String, Object> map = new TreeMap<>();
        try {
            map.put("app_key",app_key);
            map.put("timestamp", Instant.now().getEpochSecond());
            map.put("nonce",UUID.randomUUID().toString().replaceAll("-", ""));
            map.put("school_code","1621335855");//扫码的主题编码
            map.put("auth_code",params.get("code"));
            map.put("scene",1);
            StringBuilder str=new StringBuilder();
            //组织字符串
            for (Map.Entry<String,Object> entry: map.entrySet()){
                if(entry.getValue()!=null){
                    str.append(entry.getKey() + "=" + entry.getValue() + "&");
                }
            }
            str.append("key=").append(app_secret);
            log.info(str.toString());
            String sign= SignTool.signMd5Hex(str.toString(),"");
            sign=sign.toUpperCase();//加密
            log.info(sign);
            map.put("signature",sign);
            //发送post请求获取人员
            String post = HttpUtil.post(base_url, map);
            JSONObject jsonObject = JSONObject.parseObject(post);
            if(jsonObject!=null &&jsonObject.getInteger("code")==0){
                JSONObject user = jsonObject.getJSONObject("user");
                Map<String,Object> map1=new HashMap<>(2);
                map1.put("user_no",user.getString("card_number"));//学工号
                String username = user.getString("username");
                if(StringUtils.isBlank(username)){
                    username="";
                }
                map1.put("name",username);//姓名
                CabinetScreenUserVO card_number = userMapper.getUserByAccount(user.getString("card_number"));
                map1.put("user_card",card_number!=null?card_number.getUserNo():"");//卡号
                return Result.ok(map1);
            }else {
                return Result.fail(jsonObject.get("message").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("解码失败！");
        }
    }

    public static void main(String[] args) {
        Map<String, Object> map = new TreeMap<>();
        map.put("app_key","AAAAAAA");
        map.put("timestamp", Instant.now().getEpochSecond());
        map.put("nonce",UUID.randomUUID().toString().replaceAll("-", ""));
        map.put("school_code","123");//扫码的主题编码
        map.put("auth_code","BBBB");
        map.put("scene",1);
        map.put("device_no","123");//设备编号
        map.put("location","123");//地点
        StringBuilder str=new StringBuilder();
        for (Map.Entry<String,Object> entry: map.entrySet()){
            if(entry.getValue()!=null){
                str.append(entry.getKey() + "=" + entry.getValue() + "&");
            }
        }
        str.append("key=A02B94AD0678D0EB4EFE93C79EE5E429");
        System.out.println(str.toString());
        String sign=SignTool.signMd5Hex(str.toString(),"");
        sign=sign.toUpperCase();
        System.out.println(sign);
    }
}
